package com.excellence.service.impl;

import com.excellence.mapper.AnswerMapper;
import com.excellence.pojo.Answer;
import com.excellence.service.AnswerService;

import com.excellence.utils.SaveFilesPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author 沈秉璋
 * @ClassName AnswerServiceImpl
 * @Description TODO
 * @date 2024/4/8 10:49
 * @Version 1.0
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    AnswerMapper answerMapper;

    @Override
    public int deleteByKey(Long id) {
        Answer answer = answerMapper.selectByPrimaryKey(id);
        answer.setDeleted(1);
        return answerMapper.updateByPrimaryKey(answer);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return answerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Answer record, List<MultipartFile> files) {
        if (files != null){
            record.setAnswImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setAnswTime(new Date());
        return answerMapper.insert(record);
    }

    @Override
    public int insertSelective(Answer record, List<MultipartFile> files) {
        if (files != null){
            record.setAnswImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setAnswTime(new Date());
        return answerMapper.insert(record);
    }

    @Override
    public Answer selectByPrimaryKey(Long id) {
        return answerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Answer> selectAll() {
        return answerMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKeySelective(Answer record, List<MultipartFile> files) {
        if (files != null){
            record.setAnswImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setAnswTime(new Date());
        return answerMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Answer record, List<MultipartFile> files) {
        if (files != null){
            record.setAnswImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setAnswTime(new Date());
        return answerMapper.updateByPrimaryKey(record);
    }
}
